<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/6/9
  Time: 11:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>文章列表</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css">
</head>
<center>
<body>
<%--多条件查询--%>
<form class="layui-form" action="${pageContext.request.contextPath}/admin/list" method="post">

    <form class="layui-form">
        <div class="layui-inline">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-inline">
                <input type="text" name="title" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-inline">
                <select name="status" >
                    <option value="">请选择</option>
                    <option value="1">发布</option>
                    <option value="0">未发布</option>
                    </select>
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-block">
                <button class="layui-btn" lay-filter="query" lay-submit>查询</button>
            </div>
        </div>
    </form>

    <%--表格主体--%>
    <table class="layui-hide" id="roleTable" lay-filter="test"></table>
<script src="${pageContext.request.contextPath}/static/js/jquery-3.2.1.min.js"></script>
    <script>
        layui.use('table', function(){
            var table = layui.table;

            table.render({
                elem: '#test'
                ,url:'${pageContext.request.contextPath}/article/List'
                ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                    title: '提示'
                    ,layEvent: 'LAYTABLE_TIPS'
                    ,icon: 'layui-icon-tips'
                }]
                ,title: '文章列表'
                ,cols: [[
                    {type: 'checkbox', fixed: 'left'}
                    ,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
                    ,{field:'title', title:'标题', width:120, edit: 'text'}
                    ,{field:'author', title:'作者', width:120, edit: 'text'}
                    ,{field:'category', title:'所属栏目', width:120, edit: 'text'}
                    ,{field:'keywords', title:'关键字', width:120, edit: 'text'}
                    ,{field:'experience', title:'状态', width:80,
                        templet: function(res){
                            if (res.sex=="1"){
                                return "发布";
                            }
                            return "未发布";
                        }
                    }
                    ,{field:'pv', title:'访问量', width:120}
                    ,{field:'createtime', title:'创建时间', width:120}
                    ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
                ]]
                ,page: true
            });

            form.on("submit(add)",function (data) {
                //发起异步请求，新增 data.field 表单数据
                $.post("/role/add2",data.field,function (result) {
                    if(result.code==0){
                        layer.msg("新增成功")
                        //刷新父窗口的表格 parent 父窗口对象
                        parent.layui.table.reload("roleTable",{
                            page:{
                                curr:1
                            }
                        })
                        //关闭本窗口
                        //当你在iframe页面关闭自身时
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    }
                },"json")
                return false;
            })
            //头工具栏事件
            table.on('toolbar(test)', function(obj){
                var checkStatus = table.checkStatus(obj.config.id);
                switch(obj.event){
                    case 'getCheckData':
                        var data = checkStatus.data;
                        layer.alert(JSON.stringify(data));
                        break;
                    case 'getCheckLength':
                        var data = checkStatus.data;
                        layer.msg('选中了：'+ data.length + ' 个');
                        break;
                    case 'isAll':
                        layer.msg(checkStatus.isAll ? '全选': '未全选');
                        break;

                    //自定义头工具栏右侧图标 - 提示
                    case 'LAYTABLE_TIPS':
                        layer.alert('这是工具栏右侧自定义的一个图标按钮');
                        break;
                };
            });

    function goPage(num) {
        $("input[name='pageNum']").val(num);
        $("form").submit();
    }
    function deleteAdmin(id) {
        if (confirm("确认要删除此条数据！")){
        $.ajax({
            url:"${pageContext.request.contextPath}/admin/delete",
            type:"get",
            data:{"id":id},
            dataType:"json",
            success:function (result) {
                 alert("删除成功，返回结果为"+JSON.stringify(result));
                 //重新加载页面或跳转页面
                 window.location.reload();
            },
            error:function () {
                 alert("删除失败！")
            }
        })
    }
    }
        })
</script>
</body>
</center>
</html>
